{{ define "/admin/role/index" }}

{{ template "public/header" }}

<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">

                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="name" value="{{ .name }}" placeholder="角色名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>

                <div class="layui-card-body">
                    <table id="common-data" lay-filter="filterTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="rowBar">
    <button class="layui-btn layui-btn-xs" lay-event="rowBarEdit">编辑</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="rowBarDel">删除</button>
    <button class="layui-btn layui-btn-xs" lay-event="privilege">分配权限</button>
</script>

<script type="text/html" id="customBar">
    <a class="layui-btn layui-btn-xs" lay-event="customBarAdd">添加</a>
</script>

<script>
    layui.use(['laydate', 'form', 'table'], function () {
        var table = layui.table;


        var cols = [
            {
                "field": "id",
                "title": "ID",
            },
            {
                "field": "name",
                "title": "角色名",
            },
            {
                "field": "desc",
                "title": "描述",
            },
            {
                "field": "created_at",
                "title": "创建时间",
            },
            {
                "field": "",
                "title": "操作",
                "toolbar": "#rowBar"
            },
        ]
        var param={};
        param['name'] = "{{ .name }}"
        TableRenderPage(table, '/admin/role/index', cols, param);

        table.on('toolbar(filterTable)', function(obj) {
            switch(obj.event){
                case 'customBarAdd':
                    layer.open({
                        title: "添加",
                        area: ['800px', '600px'],
                        type: 2,
                        fixed: false, //不固定
                        maxmin: true,
                        content: "/admin/role/add"
                    });
                    break;
            }
        })

        // 单行操作事件
        table.on('tool(filterTable)', function(obj){
            switch(obj.event){
                case 'rowBarEdit':
                    layer.open({
                        title: "修改",
                        area: ['800px', '600px'],
                        type: 2,
                        fixed: false, //不固定
                        maxmin: true,
                        content: "/admin/role/edit?id=" + obj.data.id
                    });
                    break;
                case 'rowBarDel':
                    layer.confirm('确定删除吗', function () {
                        $.ajax({
                            url: "/admin/role/del",
                            type: 'post',
                            data: {'id': obj.data.id},
                            dataType: 'json',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                    }, function () {
                                        parent.location.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2,
                                        time: 2000//2秒关闭（如果不配置，默认是3秒）
                                    });
                                }
                            }
                        })
                    })
                    break;
                case 'privilege':
                    // 分配权限
                    layer.open({
                        title: "修改",
                        area: ['800px', '600px'],
                        type: 2,
                        fixed: false, //不固定
                        maxmin: true,
                        content: "/admin/role/privilege?id=" + obj.data.id
                    });
                    break;
            }
        })
    })
</script>
</html>
{{ end }}